<template><div><h2 id="开发扩展" tabindex="-1"><a class="header-anchor" href="#开发扩展"><span>开发扩展</span></a></h2>
<p><code v-pre>Dcat Admin</code>支持安装扩展工具来帮助丰富你的后台功能。</p>
<blockquote>
<p>需要注意的是，<code v-pre>Laravel Admin</code>原有的扩展无法直接在<code v-pre>Dcat Admin</code>中使用，但大部分扩展只需要做一些微小的调整就可以正常使用了，有兴趣的同学可以自行移植。</p>
</blockquote>
<p>如果大家在使用的过程中有在<code v-pre>Dcat Admin</code>的基础上添加一些自己的功能或者组件，不妨做成一个<code v-pre>Dcat Admin</code>扩展，这样可以给其它<code v-pre>Dcat Admin</code>使用者提供帮助，并且在其它人的使用反馈中的提升扩展的质量。</p>
<p>这篇文档将会以开发一个 <a href="https://github.com/dcat-admin/operation-log" target="_blank" rel="noopener noreferrer">操作日志扩展</a> 为例，一步一步的教大家开发一个扩展，并且发布给他人使用，最终的效果参考<a href="https://github.com/dcat-admin/operation-log" target="_blank" rel="noopener noreferrer">操作日志扩展</a>。</p>
<h2 id="开始之前" tabindex="-1"><a class="header-anchor" href="#开始之前"><span>开始之前</span></a></h2>
<p>在开始开发扩展之前，如果是<code v-pre>linux</code>环境，请先手动在项目根目录创建一个<code v-pre>dcat-admin-extensions</code>目录，并设置可读<strong>可写</strong>权限，扩展文件将会被安装在<code v-pre>dcat-admin-extensions</code>目录，请保证拥有以下几个目录的读写权限</p>
<ol>
<li><code v-pre>项目目录/dcat-admin-extensions</code> 扩展的安装目录，可以根据配置参数 <code v-pre>admin.extensions.dir</code> 进行更改</li>
<li><code v-pre>public/vendor</code> 扩展静态资源发布目录</li>
<li><code v-pre>resources/lang</code> 语言包目录</li>
</ol>
<h2 id="_1-创建扩展" tabindex="-1"><a class="header-anchor" href="#_1-创建扩展"><span>1.创建扩展</span></a></h2>
<p><code v-pre>Dcat Admin</code>的扩展是一个标准的<code v-pre>composer</code>扩展包，可以通过<code v-pre>composer</code>安装，也可以通过系统内部的<code v-pre>本地安装</code>功能直接安装。我们可以通过命令或界面创建一个新的扩展，下面分别简单介绍一下命令和界面创建的方法</p>
<p>1.通过命令创建扩展</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan admin:ext-make 扩展包名 <span class="token parameter variable">--namespace</span><span class="token operator">=</span>命名空间 <span class="token parameter variable">--theme</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>命令参数说明</p>
<ul>
<li><code v-pre>name</code> 扩展包名称，扩展的名称是一个标准的<code v-pre>composer</code>包名，请统一使用<strong>小写字母</strong> + **中划线(-)**的风格命名，标准的格式如 <code v-pre>dcat-admin/operation-log</code>，前面一部分可以是个人名称，后面一部分可以是对扩展包功能的概括词语</li>
<li><code v-pre>--namespace=</code> 扩展包命名空间，默认会根据包名自动生成，例如你的包名是<code v-pre>jiangqh/operation-log</code>，那么默认的命名空间是<code v-pre>Jiangqh/OperationLog</code></li>
<li><code v-pre>--theme</code> 是否为主题扩展</li>
</ul>
<p>那么在当前这个例子中我们运行一下命令生成扩展包</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment"># `--namespace`</span></span>
<span class="line">php artisan admin<span class="token punctuation">:</span>ext<span class="token operator">-</span>make dcat<span class="token operator">-</span>admin<span class="token operator">/</span>operation<span class="token operator">-</span>log <span class="token operator">--</span><span class="token keyword">namespace</span><span class="token operator">=</span><span class="token string double-quoted-string">"Dcat\Admin\OperationLog"</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div></div></div><p>2.通过管理页面创建扩展</p>
<p>打开扩展管理页面<code v-pre>http://localhost/admin/auth/extensions</code>，然后点击表格下面第一行的<strong>快速创建</strong>，然后输入扩展包名和命名空间即可，在实际开发中也更推荐大家使用界面创建扩展，这样更方便</p>
<p>扩展创建完成之后可以看到扩展文件夹下多了个<code v-pre>dcat-admin/extensions/dcat-admin/operation-log</code>目录，目录结构如下</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">├── <span class="token constant">README</span><span class="token operator">.</span>md</span>
<span class="line">├── composer<span class="token operator">.</span>json <span class="token comment"># composer配置文件</span></span>
<span class="line">├── version<span class="token operator">.</span>php   <span class="token comment"># 扩展包版本管理文件</span></span>
<span class="line">├── logo<span class="token operator">.</span>png      <span class="token comment"># logo</span></span>
<span class="line">├── updates       <span class="token comment"># 扩展包每个版本对应的表迁移文件</span></span>
<span class="line">├── resources </span>
<span class="line">│   ├── lang    <span class="token comment"># 语言包</span></span>
<span class="line">│   ├── assets  <span class="token comment"># 静态资源</span></span>
<span class="line">│   │   ├── css</span>
<span class="line">│   │   │   └── index<span class="token operator">.</span>css <span class="token comment"># css示例文件</span></span>
<span class="line">│   │   └── js</span>
<span class="line">│   │       └── index<span class="token operator">.</span>js  <span class="token comment"># js示例文件</span></span>
<span class="line">│   └── views</span>
<span class="line">│       └── index<span class="token operator">.</span>blade<span class="token operator">.</span>php <span class="token comment"># 视图示例文件</span></span>
<span class="line">└── src</span>
<span class="line">    ├── OperationLogServiceProvider<span class="token operator">.</span>php <span class="token comment"># 扩展包服务提供者</span></span>
<span class="line">    ├── Setting<span class="token operator">.</span>php  <span class="token comment"># 扩展设置表单</span></span>
<span class="line">    ├── Models  <span class="token comment"># 模型目录</span></span>
<span class="line">    └── Http</span>
<span class="line">        ├── routes<span class="token operator">.</span>php  <span class="token comment"># 扩展包路由文件</span></span>
<span class="line">        ├── Middleware  <span class="token comment"># 扩展包中间件文件夹</span></span>
<span class="line">        └── Controllers <span class="token comment"># 扩展包控制器</span></span>
<span class="line">            └── OperationLogController<span class="token operator">.</span>php</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>然后你还可以设置扩展的<code v-pre>logo</code>以及扩展名称，设置之后会在扩展管理页面展示，让你的扩展更具容易被人记住！</p>
<h3 id="扩展logo" tabindex="-1"><a class="header-anchor" href="#扩展logo"><span>扩展logo</span></a></h3>
<p>扩展<code v-pre>logo</code>必须放在扩展的根目录，并且文件名必须是<code v-pre>logo.png</code>，建议尺寸<code v-pre>100x100</code>。</p>
<h3 id="扩展名称" tabindex="-1"><a class="header-anchor" href="#扩展名称"><span>扩展名称</span></a></h3>
<p>扩展名称需要修改<code v-pre>composer.json</code>里面的<code v-pre>alias</code>参数，如果不设置则默认展示包名</p>
<h2 id="_2-启用扩展" tabindex="-1"><a class="header-anchor" href="#_2-启用扩展"><span>2.启用扩展</span></a></h2>
<p>扩展创建成功之后就可以在管理页面<code v-pre>http://localhost/admin/auth/extensions</code> 看到新创建的扩展了，效果如下</p>
<p><img src="@source/dcat-admin-2x/assets/images/d9576cbf-5d77-4cbc-ae52-e9a2c64ef06e-zRBcsCFn88.png" alt=""></p>
<p>然后我们分别点击扩展对应的 <code v-pre>更新至1.0.0版本</code> 以及 <code v-pre>启用</code> 按钮，就可以使这个扩展生效了。<br>
新创建的扩展会生成一个默认的控制器，在这个例子中，我们可以尝试访问<code v-pre>http://localhost:8000/admin/operation-log</code>，如果可以正常访问则说明扩展启用成功。</p>
<h2 id="_3-功能开发" tabindex="-1"><a class="header-anchor" href="#_3-功能开发"><span>3.功能开发</span></a></h2>
<p>这个扩展的功能主要是用来记录用户的操作记录，然后提供一个查看记录的页面，然后我们可以把默认创建的扩展文件中用不到的给清理掉，清理后的目录结构如下</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">├── <span class="token constant">README</span><span class="token operator">.</span>md</span>
<span class="line">├── composer<span class="token operator">.</span>json <span class="token comment"># composer配置文件</span></span>
<span class="line">├── version<span class="token operator">.</span>php   <span class="token comment"># 扩展包版本管理文件</span></span>
<span class="line">├── logo<span class="token operator">.</span>png      <span class="token comment"># 扩展包logo</span></span>
<span class="line">├── updates       <span class="token comment"># 扩展包每个版本对应的表迁移文件</span></span>
<span class="line">├── resources </span>
<span class="line">│   └── lang  <span class="token comment"># 语言包</span></span>
<span class="line">└── src</span>
<span class="line">    ├── OperationLogServiceProvider<span class="token operator">.</span>php <span class="token comment"># 扩展包服务提供者</span></span>
<span class="line">    ├── Setting<span class="token operator">.</span>php  <span class="token comment"># 扩展设置表单</span></span>
<span class="line">    ├── Models  <span class="token comment"># 模型目录</span></span>
<span class="line">    └── Http</span>
<span class="line">        ├── routes<span class="token operator">.</span>php  <span class="token comment"># 扩展包路由文件</span></span>
<span class="line">        ├── Middleware  <span class="token comment"># 扩展包中间件文件夹</span></span>
<span class="line">        └── Controllers <span class="token comment"># 扩展包控制器</span></span>
<span class="line">            └── OperationLogController<span class="token operator">.</span>php</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>接下来就正式进入功能开发部分</p>
<h3 id="创建迁移文件-migration" tabindex="-1"><a class="header-anchor" href="#创建迁移文件-migration"><span>创建迁移文件 (migration)</span></a></h3>
<p>首先我们需要创建一个表迁移文件，运行命令 <code v-pre>php artisan make:migration CreateOperationLogTable</code>，然后写入文件内容如下</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Database<span class="token punctuation">\</span>Migrations<span class="token punctuation">\</span>Migration</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Database<span class="token punctuation">\</span>Schema<span class="token punctuation">\</span>Blueprint</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Schema</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">CreateOperationLogTable</span> <span class="token keyword">extends</span> <span class="token class-name">Migration</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// 这里可以指定你的数据库连接</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">getConnection</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token function">config</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'database.connection'</span><span class="token punctuation">)</span> <span class="token operator">?</span><span class="token punctuation">:</span> <span class="token function">config</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'database.default'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">up</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span> <span class="token class-name static-context">Schema</span><span class="token operator">::</span><span class="token function">hasTable</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'admin_operation_log'</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">            <span class="token class-name static-context">Schema</span><span class="token operator">::</span><span class="token function">create</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'admin_operation_log'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Blueprint</span> <span class="token variable">$table</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">                <span class="token variable">$table</span><span class="token operator">-></span><span class="token function">bigIncrements</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'id'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">unsigned</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">                <span class="token variable">$table</span><span class="token operator">-></span><span class="token function">bigInteger</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'user_id'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">                <span class="token variable">$table</span><span class="token operator">-></span><span class="token keyword type-declaration">string</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'path'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">                <span class="token variable">$table</span><span class="token operator">-></span><span class="token keyword type-declaration">string</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'method'</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">                <span class="token variable">$table</span><span class="token operator">-></span><span class="token keyword type-declaration">string</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'ip'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">                <span class="token variable">$table</span><span class="token operator">-></span><span class="token function">text</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'input'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">                <span class="token variable">$table</span><span class="token operator">-></span><span class="token function">index</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'user_id'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">                <span class="token variable">$table</span><span class="token operator">-></span><span class="token function">timestamps</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">            <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token punctuation">}</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">down</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token class-name static-context">Schema</span><span class="token operator">::</span><span class="token function">dropIfExists</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'admin_operation_log'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>然后把文件移动到 <code v-pre>扩展目录/updates</code> 目录，并重命名为 <code v-pre>create_opration_log_table.php</code>。最后我们需要修改扩展的版本管理文件 <code v-pre>扩展目录/version.php</code>，写入迁移文件的文件名，最终内容如下</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'1.0.0'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'版本变化描述1'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'版本变化描述2'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'create_opration_log_table.php'</span><span class="token punctuation">,</span> <span class="token comment">// 迁移文件名称，安装或更新版本时会自动执行</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>关于扩展的版本管理更详细的介绍，请前往<a href="https://learnku.com/docs/dcat-admin/2.x/basic-use/extension-f.md#version" target="_blank" rel="noopener noreferrer">扩展 - 版本管理</a>章节查看。</p>
<h3 id="模型、控制器和路由" tabindex="-1"><a class="header-anchor" href="#模型、控制器和路由"><span>模型、控制器和路由</span></a></h3>
<p>创建模型文件 <code v-pre>扩展目录/src/Models/OperationLog</code>，模型内容点击<a href="https://github.com/dcat-admin/operation-log/blob/master/src/Models/OperationLog.php" target="_blank" rel="noopener noreferrer">OperationLog.php</a>查看；</p>
<p>然后修改我们的控制器<code v-pre>扩展目录/src/Http/Controllers/OperationLogController.php</code>，控制器内容点击<a href="https://github.com/dcat-admin/operation-log/blob/master/src/Http/Controllers/LogController.php" target="_blank" rel="noopener noreferrer">LogController.php</a>查看；</p>
<p>最后需要修改我们的路由文件，尽量让你的路由不与其他路由产生冲突</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Dcat<span class="token punctuation">\</span>Admin<span class="token punctuation">\</span>OperationLog<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Controllers</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Route</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Route</span><span class="token operator">::</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'auth/operation-logs'</span><span class="token punctuation">,</span> <span class="token class-name class-name-fully-qualified static-context">Controllers<span class="token punctuation">\</span>OperationLogController</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token operator">.</span><span class="token string single-quoted-string">'@index'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">name</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'dcat-admin.operation-log.index'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token class-name static-context">Route</span><span class="token operator">::</span><span class="token function">delete</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'auth/operation-logs/{id}'</span><span class="token punctuation">,</span> <span class="token class-name class-name-fully-qualified static-context">Controllers<span class="token punctuation">\</span>OperationLogController</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token operator">.</span><span class="token string single-quoted-string">'@destroy'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">name</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'dcat-admin.operation-log.destroy'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="语言包" tabindex="-1"><a class="header-anchor" href="#语言包"><span>语言包</span></a></h3>
<p>在控制器中，我们可以让一些文本描述支持语言包翻译功能，在这个例子中我们以<code v-pre>en</code>以及<code v-pre>zh_CN</code>两种语言为例，在<code v-pre>扩展包/resources/lang</code>目录下分别创建<code v-pre>en/log.php</code>和<code v-pre>zh_CN/log.php</code>文件，并写入以下内容</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// en</span></span>
<span class="line"><span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'title'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Operation Log'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'setting_title'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Operation Log'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// zh_CN</span></span>
<span class="line"><span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'title'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'操作日志'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'setting_title'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'操作日志'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>最后在控制器中可以通过下面的方法访问语言包内容，关于多语言的更多用法可以参考laravel官方文档</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Dcat<span class="token punctuation">\</span>Admin<span class="token punctuation">\</span>OperationLog<span class="token punctuation">\</span>OperationLogServiceProvider</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">OperationLogServiceProvider</span><span class="token operator">::</span><span class="token function">trans</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'log.title'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token class-name static-context">OperationLogServiceProvider</span><span class="token operator">::</span><span class="token function">trans</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'log.setting_title'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="定义菜单" tabindex="-1"><a class="header-anchor" href="#定义菜单"><span>定义菜单</span></a></h3>
<p>接下来我们还需要给我们的扩展生成菜单，打开<code v-pre>扩展目录/src/OperationLogServiceProvider.php</code>，然后修改内容如下</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">OperationLogServiceProvider</span> <span class="token keyword">extends</span> <span class="token class-name">ServiceProvider</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// 定义菜单</span></span>
<span class="line">    <span class="token keyword">protected</span> <span class="token variable">$menu</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'title'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Operation Log'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'uri'</span>   <span class="token operator">=></span> <span class="token string single-quoted-string">'auth/operation-logs'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'icon'</span>  <span class="token operator">=></span> <span class="token string single-quoted-string">''</span><span class="token punctuation">,</span> <span class="token comment">// 图标可以留空</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">settingForm</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">Setting</span><span class="token punctuation">(</span><span class="token variable">$this</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果你想注册带有层级关系的菜单，可以通过下面这种方式注册</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// 注册菜单</span></span>
<span class="line"><span class="token keyword">protected</span> <span class="token variable">$menu</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'title'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Operation Log'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'uri'</span>   <span class="token operator">=></span> <span class="token string single-quoted-string">''</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'icon'</span>  <span class="token operator">=></span> <span class="token string single-quoted-string">'feather icon-x'</span><span class="token punctuation">,</span> </span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'parent'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Operation Log'</span><span class="token punctuation">,</span> <span class="token comment">// 指定父级菜单</span></span>
<span class="line">        <span class="token string single-quoted-string">'title'</span>  <span class="token operator">=></span> <span class="token string single-quoted-string">'List'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'uri'</span>    <span class="token operator">=></span> <span class="token string single-quoted-string">'auth/operation-logs'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="测试扩展" tabindex="-1"><a class="header-anchor" href="#测试扩展"><span>测试扩展</span></a></h3>
<p>上面的步骤都完成之后，我们可以先开始测试一下上面的功能，验证下是否有错误，然后再进行后续的开发。</p>
<p>由于刚开始创建扩展的时候，我们已经安装并启用了这个扩展，所以此处我们要先<strong>卸载</strong>当前扩展，再重新更新到<code v-pre>1.0.0</code>版本，这样数据表和菜单才会被创建。</p>
<blockquote>
<p><strong>卸载</strong>功能会删除扩展的数据或数据表，请谨慎操作，以免造成数据丢失！！！</p>
</blockquote>
<p>打开扩展管理页面<code v-pre>http://域名/admin/auth/extensions</code>，找到当前扩展，鼠标移动到扩展行，点击 <code v-pre>卸载</code> 按钮并确认，然后重新点击<code v-pre>更新至1.0.0版本</code> 以及 <code v-pre>启用</code> 按钮，<br>
最后 <code v-pre>F5</code> 刷新浏览器即可看到新创建的菜单，点击菜单可访问操作日志管理页面<code v-pre>admin/auth/operation-logs</code>。</p>
<h3 id="注册中间件" tabindex="-1"><a class="header-anchor" href="#注册中间件"><span>注册中间件</span></a></h3>
<p>现在我们的扩展还需要一个中间件来记录用户的操作，创建文件 <code v-pre>扩展目录/src/Http/Middleware/LogOperation.php</code>，文件内容点击<a href="https://github.com/dcat-admin/operation-log/blob/master/src/Http/Middleware/LogOperation.php" target="_blank" rel="noopener noreferrer">LogOperation.php</a>查看。</p>
<p>然后我们需要注册这个中间件，让这个中间件生效，打开<code v-pre>扩展目录/src/OperationLogServiceProvider.php</code>，然后修改内容如下</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">OperationLogServiceProvider</span> <span class="token keyword">extends</span> <span class="token class-name">ServiceProvider</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">protected</span> <span class="token variable">$middleware</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'middle'</span> <span class="token operator">=></span> <span class="token punctuation">[</span> <span class="token comment">// 注册中间件</span></span>
<span class="line">            <span class="token class-name static-context">LogOperation</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token keyword">protected</span> <span class="token variable">$menu</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'title'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Operation Log'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'uri'</span>   <span class="token operator">=></span> <span class="token string single-quoted-string">'auth/operation-logs'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">settingForm</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">Setting</span><span class="token punctuation">(</span><span class="token variable">$this</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><code v-pre>$middleware</code>属性中注册的中间件最后会合并到配置参数<code v-pre>admin.route.middleware</code>中，中间件注册支持以下三种类型注册</p>
<ol>
<li><code v-pre>before</code> 中间件会在最前面，也就是最先执行</li>
<li><code v-pre>middle</code> 中间件会在<code v-pre>admin.auth</code>（登陆鉴权中）和<code v-pre>admin.permission</code>（权限判断）两个中间件<strong>之间</strong>执行</li>
<li><code v-pre>after</code> 中间件会在最后执行</li>
</ol>
<p>在这个例子中，操作日志记录用户操作时显然需要记录登陆用户的信息，所以中间件必须在 <code v-pre>admin.auth</code> 中间件之后执行，这样才能拿到登陆用户数据；<br>
并且无权限的操作也需要记录，所以必须在 <code v-pre>admin.permission</code> 中间件之前执行，所以只有注册为 <code v-pre>middle</code> 类型的中间件才能满足上述要求！</p>
<p>注册完中间件之后，我们随意访问一下系统中的其他页面（除了操作日志管理页面），然后再访问操作日志管理页面，就可以看到用户的操作日志了，到这里插件基本开发完毕。</p>
<h3 id="配置参数-设置" tabindex="-1"><a class="header-anchor" href="#配置参数-设置"><span>配置参数（设置）</span></a></h3>
<p>在当前这个例子中，我们需要让用户能配置一些自定义参数（比如配置不需要记录操作日志的路由），所以我们还需要一个 <code v-pre>配置表单</code> 让用户能通过页面直接配置相关参数，<br>
我们需要在 <code v-pre>OperationLogServiceProvider</code> 类里面的 <code v-pre>settingForm</code> 返回这个配置表单的对象</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">OperationLogServiceProvider</span> <span class="token keyword">extends</span> <span class="token class-name">ServiceProvider</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token operator">...</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// 返回配置表单对象，如果不需要保存配置参数，则请删除这个方法  </span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">settingForm</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">Setting</span><span class="token punctuation">(</span><span class="token variable">$this</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>然后我们需要修改配置表单类 <code v-pre>扩展目录/src/Setting.php</code> 如下</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">namespace</span> <span class="token package">Dcat<span class="token punctuation">\</span>Admin<span class="token punctuation">\</span>OperationLog</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Dcat<span class="token punctuation">\</span>Admin<span class="token punctuation">\</span>Extend<span class="token punctuation">\</span>Setting</span> <span class="token keyword">as</span> Form<span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Dcat<span class="token punctuation">\</span>Admin<span class="token punctuation">\</span>OperationLog<span class="token punctuation">\</span>Models<span class="token punctuation">\</span>OperationLog</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Dcat<span class="token punctuation">\</span>Admin<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Helper</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">Setting</span> <span class="token keyword">extends</span> <span class="token class-name">Form</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// 返回表单弹窗标题</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">title</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">trans</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'log.title'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// 格式化待保存的配置参数值</span></span>
<span class="line">    <span class="token keyword">protected</span> <span class="token keyword">function</span> <span class="token function-definition function">formatInput</span><span class="token punctuation">(</span><span class="token keyword type-hint">array</span> <span class="token variable">$input</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token comment">// 转化为数组，注意如果这里保存的时候是数组，那么读取出来的时候也是数组</span></span>
<span class="line">        <span class="token variable">$input</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'except'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token class-name static-context">Helper</span><span class="token operator">::</span><span class="token function">array</span><span class="token punctuation">(</span><span class="token variable">$input</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'except'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token variable">$input</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'allowed_methods'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token class-name static-context">Helper</span><span class="token operator">::</span><span class="token function">array</span><span class="token punctuation">(</span><span class="token variable">$input</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'allowed_methods'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token keyword">return</span> <span class="token variable">$input</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">form</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token comment">// 定义表单字段</span></span>
<span class="line">        <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">tags</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'except'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">multipleSelect</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'allowed_methods'</span><span class="token punctuation">)</span></span>
<span class="line">            <span class="token operator">-></span><span class="token function">options</span><span class="token punctuation">(</span><span class="token function">array_combine</span><span class="token punctuation">(</span><span class="token class-name static-context">OperationLog</span><span class="token operator">::</span><span class="token variable">$methods</span><span class="token punctuation">,</span> <span class="token class-name static-context">OperationLog</span><span class="token operator">::</span><span class="token variable">$methods</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">tags</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'secret_fields'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>以上设置完成之后我们就可以在扩展管理页面保存自定义参数了</p>
<p><img src="@source/dcat-admin-2x/assets/images/46eecb75-ea00-467f-835e-8fddd98258bd-ikrmZD1DWm.png" alt="undefined"></p>
<p>配置参数读取用法如下，我们可以在中间件 <code v-pre>LogOperation</code> 中使用这些参数</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Dcat<span class="token punctuation">\</span>Admin<span class="token punctuation">\</span>OperationLog<span class="token punctuation">\</span>OperationLogServiceProvider</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 读取配置参数</span></span>
<span class="line"><span class="token variable">$except</span> <span class="token operator">=</span> <span class="token class-name static-context">OperationLogServiceProvider</span><span class="token operator">::</span><span class="token function">setting</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'except'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$allowedMethods</span> <span class="token operator">=</span> <span class="token class-name static-context">OperationLogServiceProvider</span><span class="token operator">::</span><span class="token function">setting</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'allowed_methods'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$secretFields</span> <span class="token operator">=</span> <span class="token class-name static-context">OperationLogServiceProvider</span><span class="token operator">::</span><span class="token function">setting</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'secret_fields'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="服务注册与初始化" tabindex="-1"><a class="header-anchor" href="#服务注册与初始化"><span>服务注册与初始化</span></a></h3>
<p>由于当前这个例子中没有使用到服务注册与初始化相关功能，所以这部分内容先略过，有相关需要的同学可以参考<a href="https://learnku.com/docs/dcat-admin/2.x/basic-use/extension-f.md#service" target="_blank" rel="noopener noreferrer">扩展 - 服务注册与初始化</a>章节。</p>
<h3 id="视图-view" tabindex="-1"><a class="header-anchor" href="#视图-view"><span>视图 (view)</span></a></h3>
<p>由于当前这个例子中没有使用到自定义静态资源的功能，所以这部分内容先略过，有相关需要的同学可以参考<a href="https://learnku.com/docs/dcat-admin/2.x/extended-basic-usage/9691#view" target="_blank" rel="noopener noreferrer">扩展 - 视图</a>章节。</p>
<h3 id="静态资源" tabindex="-1"><a class="header-anchor" href="#静态资源"><span>静态资源</span></a></h3>
<p>由于当前这个例子中没有使用到自定义静态资源的功能，所以这部分内容先略过，有相关需要的同学可以参考<a href="https://learnku.com/docs/dcat-admin/2.x/extended-basic-usage/9691#assets" target="_blank" rel="noopener noreferrer">扩展 - 静态资源</a>章节。</p>
<h3 id="修改-composer-json-readme-md" tabindex="-1"><a class="header-anchor" href="#修改-composer-json-readme-md"><span>修改 composer.json &amp; README.md</span></a></h3>
<p>代码部分完成之后，需要修改<code v-pre>composer.json</code>里面的内容，将<code v-pre>description</code>、<code v-pre>keywords</code>、<code v-pre>license</code>、<code v-pre>authors</code>等内容替换为你的信息，然后不要忘记完善<code v-pre>README.md</code>，补充使用文档等相关信息。</p>
<h3 id="发布扩展" tabindex="-1"><a class="header-anchor" href="#发布扩展"><span>发布扩展</span></a></h3>
<h4 id="上传应用市场" tabindex="-1"><a class="header-anchor" href="#上传应用市场"><span>上传应用市场</span></a></h4>
<p>正式版发布后会上线应用市场功能，开发者可以把扩展发布到应用市场，然后用户就可以通过页面直接安装使用，敬请期待…</p>
<h4 id="上传到github" tabindex="-1"><a class="header-anchor" href="#上传到github"><span>上传到Github</span></a></h4>
<p>先登录你的Github，创建一个仓库，然后按照页面上的提示把你的代码push上去</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">git init</span>
<span class="line">git remote add origin https<span class="token punctuation">:</span><span class="token comment">//github.com/&lt;your-name>/&lt;your-repository>.git</span></span>
<span class="line">git add <span class="token operator">.</span></span>
<span class="line">git commit <span class="token operator">-</span>am <span class="token string double-quoted-string">"Initial commit."</span></span>
<span class="line">git push origin master</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="发布到packagist-org" tabindex="-1"><a class="header-anchor" href="#发布到packagist-org"><span>发布到Packagist.org</span></a></h4>
<p>接下来就是发布你的项目到<code v-pre>Packagist.org</code>，如果没有账号的话，先注册一个，然后打开顶部导航的<code v-pre>Submit</code>, 填入仓库地址提交</p>
<p>默认情况下，当您推送新代码时，<code v-pre>Packagist.org</code>不会自动更新，所以，您需要创建一个GitHub服务钩子， 你也可以使用点击页面上的<code v-pre>Update</code>按钮手动更新它，但我建议自动执行这个过程</p>
<p>提交之后，由于各地的镜像同步时间的延迟，可能在用 <code v-pre>composer</code> 安装的时候，会暂时找不到你的项目，这个时候可能需要等待同步完成</p>
<p>发布完成之后就可以通过 <code v-pre>composer</code> 安装你的扩展了</p>
</div></template>


